package com.hnevc.foodserver.controller;

import com.hnevc.foodserver.bean.Types;
import com.hnevc.foodserver.bean.Users;
import com.hnevc.foodserver.dao.TypesDao;
import com.hnevc.foodserver.service.TypesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.utility.RandomString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/types")
@Api(tags = "菜品类别接口")
public class TypesController {
    @Autowired
    TypesService typesService;
    @Autowired
    private TypesDao typesDao;

    @PostMapping("/list")
    @ApiOperation(value = "分类列表")
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Types> all = typesService.list();
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", all);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    @PostMapping("/findone")
    @ApiOperation(value = "获取单个分类信息")
    public Map<String, Object> findone(@RequestBody Types types) {
        Map<String, Object> map = new HashMap<>();
        try {
            Types types1 = typesService.findone(types);
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", types1);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }
    @PostMapping("/add")
    @ApiOperation(value = "添加一个类别")
    public Map<String, Object> add(@RequestBody Types types) {
        Map<String, Object> map = new HashMap<>();
        try {
            Types types1 = typesService.add(types);
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", types1);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }
    @PostMapping("/delete")
    @ApiOperation(value = "获取单个分类信息")
    public Map<String, Object> deleteById(@RequestBody Types types) {
        Map<String, Object> map = new HashMap<>();
        try {
            Types types1 = typesService.deleteById(types);
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", types1);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }
    @PostMapping("/update")
    @ApiOperation(value = "修改单个分类信息")
    public Map<String, Object> update(@RequestBody Types types) {
        Map<String, Object> map = new HashMap<>();
        try {
            Types types1 = typesService.update(types);
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", types1);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }

    @PostMapping("/update_typeicon")
    @ApiOperation(value = "修改单个分类图标")
    public Map<String,Object> uploadHeadPic(@RequestParam("file") MultipartFile file,
                                            @RequestParam("id") Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
            byte[] bytes = file.getBytes();
            String filePath =  new RandomString(10).nextString() + file.getOriginalFilename();
            Path path = Paths.get("D://upload//" +filePath);
            Files.write(path, bytes);
            map.put("success",true);
            map.put("msg","成功");
            //TODO 更新到数据库
            // 1. 获取用户服务对象，
            Types types = new Types();
            types.setId( id);
            types.setIcon(filePath);
            Types types1 = typesService.save(types);
            map.put("data",types1);

        } catch (IOException e) {
            e.printStackTrace();
            map.put("success",false);
            map.put("msg","文件读取错误");
        }
        return map;
    }
}
